package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanglishen
 * @since 2020-11-14
 */
@RestController
@RequestMapping("/system/cfg")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "通过用户ID查询菜单列表")
    @GetMapping("/menu")
    public List<Menu> getMenusByAdminId(){
        return menuService.getMenusByAdminId();
    }

    @ApiOperation(value = "查询所有菜单数据")
    @GetMapping("/allMenu")
    public List<Menu> allMenu(){
        return menuService.getAllMenus();
    }

    @ApiOperation(value = "修改菜单数据")
    @PostMapping("/editMenu")
    public RespBean editMenu(@RequestBody Menu menu){
        if (menuService.updateById(menu)){
            return RespBean.success("修改菜单数据成功");
        }
        return RespBean.error("修改菜单数据失败");
    }

    @ApiOperation(value = "修改菜单数据")
    @PostMapping("/addMenu")
    public RespBean addMenu(@RequestBody Menu menu){
        if (menuService.save(menu)){
            return RespBean.success("菜单数据添加成功");
        }
        return RespBean.error("菜单数据添加失败");
    }

    @ApiOperation(value = "删除菜单数据")
    @DeleteMapping("/deleteMenu/{id}")
    public RespBean deleteMenu(@PathVariable Integer id){
        menuService.removeById(id);
        Set<String> keys = redisTemplate.keys("menu_" + "*");
        redisTemplate.delete(keys);
        return RespBean.success("删除成功");
    }

}
